using System;
using System.Collections.Generic;
using System.Text;
using Manzana;

namespace MicroFractal.iTunesBrowser.Core.Device
{
    public class IPhoneDevice : BaseDevice
    {
        #region Constructors

        public IPhoneDevice(iPhone iphone)
            : base(iphone.Device.serial, iphone.Device.serial)
        {
            _iphone = iphone;
            base.DirectorySeparatorChar = '/';
            base.DBRootPath = "private/var/mobile/Media";
        }

        #endregion

        #region Properties

        public override bool IsConnected
        {
            get
            {
                return _iphone.IsConnected;
            }
            protected set
            {
            }
        }

        private iPhone _iphone;

        protected override string ControlFolder
        {
            get { return "iTunes_Control"; }
        }

        #endregion

        #region Methods

        public override System.IO.MemoryStream GetDBFile()
        {
            if (_iphone.Exists(this.GetDBFilePath()))
            {
                iPhoneFile f = iPhoneFile.OpenRead(_iphone, this.GetDBFilePath());
                System.IO.MemoryStream m = new System.IO.MemoryStream();
                byte[] b = new byte[0x2000];
                int count = f.Read(b, 0, b.Length);
                while (count > 0)
                {
                    m.Write(b, 0, count);
                    count = f.Read(b, 0, b.Length);
                }
                f.Close();
                return m;
            }
            return null;
        }

        #endregion
        
        public override bool CanCancelDownload
        {
            get
            {
                return true;
            }
            protected set
            {
            }
        }

        protected override void Download(MicroFractal.iTunesBrowser.Business.Song song, string downloadPath)
        {
            if (_iphone.Exists(this.GetFilePath(song)))
            {
                iPhoneFile f = iPhoneFile.OpenRead(_iphone, this.GetFilePath(song));
                System.IO.FileStream file = new System.IO.FileStream(downloadPath + "\\" + GetSongFileName(song),System.IO.FileMode.CreateNew);
                try
                {
                    byte[] b = new byte[0x2000];
                    int count = f.Read(b, 0, b.Length);
                    while (count > 0)
                    {
                        if (_cancelDownload)
                            throw new Exception();

                        file.Write(b, 0, count);
                        count = f.Read(b, 0, b.Length);
                    }
                }
                catch { }
                finally
                {
                    f.Close();
                    file.Flush();
                    file.Close();
                }
            }
        }

        public override System.IO.MemoryStream GetDBFile(bool refresh)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override bool CancelDownload()
        {
            return base.CancelDownload();
            _cancelDownload = true;
        }

        private bool _cancelDownload = false;

        public override string GetFilePath(MicroFractal.iTunesBrowser.Business.Song song)
        {
            string filePath = song.Location;
            filePath = filePath.Replace(':', this.DirectorySeparatorChar);
            return DBRootPath + filePath;
        }

    }
}
